#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

char* getCard(int cardNo);

int main() {
    int i; //loop counter
    int r; //initialization variable
    int a; //testing variable
    int b; //testing variable
    int c; //testing variable
    int bonus = 0; //placeholder for bonus
    int oppositePlayer = 0; //opposite player
    int partialPass = 1; //for compound tests
    int failcount = 0; //track tests passed
    int passcount = 0; //track tests failed
    int check; //holds result returned from test function
    int seed = 1000;
    int numPlayer = 2;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;
    
    /*
     * test that playing council_room makes player draw 4 and discard 1 for
     * a net gain of 3, and that opposing player draws 1 card.
     * 
     */
    printf("\nTesting council_room:\n\n");

    for (i = 0; i < numPlayer; i++){

        printf("Testing council_room card played by player %d:\n", i);
        memset(&G, 23, sizeof(struct gameState));   // clear the game state
        r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
        
        //check for empty hand
        if (G.handCount[i] == 0){
            G.handCount[i] = 1;
        }

        G.hand[i][0] = council_room; //put council_room @ position 1        
        G.whoseTurn = i; //set turn to current player

        //switch for opposite players
        if (i == 0){
            oppositePlayer = 1;
        }
        else {
            oppositePlayer = 0;
        }

        a = G.handCount[i];
        b = G.handCount[oppositePlayer];
        c = G.numBuys;
        

        check = cardEffect(council_room, -1, -1, -1, &G, handCard(0, &G), &bonus);

        printf("playing council_room set player %d's handcount to %d, ",
        i, G.handCount[i]);
        printf("Expected: %d\n", a + 3);


        printf("playing council_room set player %d's handcount to %d, ",
        oppositePlayer, G.handCount[oppositePlayer]);
        printf("Expected: %d\n", b + 1);

        printf("cardEffect set numBuys to %d, ", G.numBuys);
        printf("Expected: %d", c + 1);

        printf("cardEffect() returned %d after playing council_room", check);
        printf(", Expected: 0\n");

        if(G.handCount[oppositePlayer] != b + 1){
            partialPass = 0;
        }
        if(G.handCount[i] != a + 3){
            partialPass = 0;
        }
        if(G.numBuys != c + 1){
            partialPass = 0;
        }
        if(check != 0){
            partialPass = 0;
        }
        if (partialPass == 1) {
            printf("TEST PASSED\n\n");
            passcount++;
        }
        else{
            printf("TEST FAILED\n\n");
            failcount++;
        }
    }

    if (failcount == 0){
        printf("All tests passed!\n\n");
    }
    else{
        printf("%d tests failed. %d tests passed.\n", failcount, passcount);
        printf("Review log above for details.\n\n");
    }
    return 0;
}

/*
 * Function to convert number to string description
 */
char* getCard(int cardNo){
    if (cardNo == curse){
        return "curse";
    }
    if (cardNo == estate){
        return "estate";
    }
    if (cardNo == duchy){
        return "duchy";
    }
    if (cardNo == province){
        return "province";
    }
    if (cardNo == copper){
        return "copper";
    }
    if (cardNo == silver){
        return "silver";
    }
    if (cardNo == gold){
        return "gold";
    }
    if (cardNo == adventurer){
        return "adventurer";
    }
    if (cardNo == council_room){
        return "council_room";
    }
    if (cardNo == feast){
        return "feast";
    }
    if (cardNo == gardens){
        return "gardens";
    }
    if (cardNo == mine){
        return "mine";
    }
    if (cardNo == remodel){
        return "remodel";
    }
    if (cardNo == smithy){
        return "smithy";
    }
    if (cardNo == village){
        return "village";
    }
    if (cardNo == baron){
        return "baron";
    }
    if (cardNo == great_hall){
        return "great_hall";
    }
    if (cardNo == minion){
        return "minion";
    }
    if (cardNo == steward){
        return "steward";
    }
    if (cardNo == tribute){
        return "tribute";
    }
    if (cardNo == ambassador){
        return "ambassador";
    }
    if (cardNo == cutpurse){
        return "cutpurse";
    }
    if (cardNo == embargo){
        return "embargo";
    }
    if (cardNo == outpost){
        return "outpost";
    }
    if (cardNo == salvager){
        return "salvager";
    }
    if (cardNo == sea_hag){
        return "sea_hag";
    }
    if (cardNo == treasure_map){
        return "treasure_map";
    }
    return "fail";
}

